package thread;

import java.util.concurrent.Executor;

public class Lock {
    public int globalNum = 0;
    public synchronized void add() {
        this.globalNum++;
        this.globalNum++;
        this.globalNum++;
        this.globalNum++;
        this.globalNum++;
    }
    public void add2() {
        synchronized (new Integer(1)) {
            for (int i = 0; i < 10000; i++) {
                int c = i;
                int b=c;

            };

            this.globalNum++;
            this.globalNum++;
            this.globalNum++;
            this.globalNum++;
            this.globalNum++;
        }
    }

    public static void main(String[] args) throws Exception{
        Executor executor = new Executor() {
            @Override
            public void execute(Runnable command) {
                Thread thread = new Thread(command);
                thread.start();
            }
        };
        Lock lock = new Lock();
        for (int i = 0; i < 100000; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    lock.add2();
                }
            });
        }
        Thread.currentThread().sleep(1000);
        System.out.println(lock.globalNum);
    }
}
